/*
 * @Author: zhiming
 * @Date: 2020-07-20 14:56:16
 * @LastEditTime: 2020-08-04 17:32:54
 * @LastEditors: Please set LastEditors
 * @Description: 工具函数类
 * @FilePath: \shopping_mall_app\src\assets\js\utils\index.js
 */
import Echo from '../libs/echo.js';
import moment from 'moment';

//获取路由参数
export function localParam (search, hash) {
    search = search || window.location.search;
    hash = hash || window.location.hash;
    var fn = function(str, reg) {
        if (str) {
            var data = {};
            str.replace(reg, function($0, $1, $2, $3) {
                data[$1] = $3;
            });
            return data;
        }
    };
    return {
        search : fn(search, new RegExp("([^?=&]+)(=([^&]*))?", "g")) || {},
        hash : fn(hash, new RegExp("([^#=&]+)(=([^&]*))?", "g")) || {}
    };
}

//页面置顶
export function setScrollTop(val=0){
    setTimeout(()=>{
        document.body.scrollTop=val;
        document.documentElement.scrollTop=val;
    },300);
}

//图片懒加载
export function lazyImg(){
    Echo.init({
        offset: 50, //离可视区域多少像素的图片可以被加载
        throttle: 0 //图片延迟多少毫秒加载
    });
}

//页面跳转
export function goPage(url){
    this.props.history.push(window.base.config.path+url);
}

//数据深拷贝
export function deepClone(obj, hash = new WeakMap()){
    if (obj === null) return obj; // 如果是null或者undefined我就不进行拷贝操作
    if (obj instanceof Date) return new Date(obj);
    if (obj instanceof RegExp) return new RegExp(obj);
    // 可能是对象或者普通的值  如果是函数的话是不需要深拷贝
    if (typeof obj !== "object") return obj;
    // 是对象的话就要进行深拷贝
    if (hash.get(obj)) return hash.get(obj);
    let cloneObj = new obj.constructor();
    // 找到的是所属类原型上的constructor,而原型上的 constructor指向的是当前类本身
    hash.set(obj, cloneObj);
    // eslint-disable-next-line
    for (let key in obj) {
        if (obj.hasOwnProperty(key)) {
        // 实现一个递归拷贝
        cloneObj[key] = deepClone(obj[key], hash);
        }
    }
    return cloneObj;
}

//组织冒泡事件
export function preventBubble(e){
    e.stopPropagation();
}

//处理台风时间格式
export function getTimeStr(dt){
    return `${dt.slice(0,4)+"-"+dt.slice(4,6)+"-"+dt.slice(6,8)+" "+dt.slice(8,10)+":"+dt.slice(10,12)}:00`;
}

//判断ie版本
export function getIEVersion(){
    const userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
    const isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1; //判断是否IE<11浏览器
    const isEdge = userAgent.indexOf("Edge") > -1 && !isIE; //判断是否IE的Edge浏览器
    const isIE11 = userAgent.indexOf('Trident') > -1 && userAgent.indexOf("rv:11.0") > -1;
    if (isIE) {
        const reIE = new RegExp("MSIE (\\d+\\.\\d+);");
        reIE.test(userAgent);
        const fIEVersion = parseFloat(RegExp["$1"]);
        if (fIEVersion === 7) {
            return 7;
        } else if (fIEVersion === 8) {
            return 8;
        } else if (fIEVersion === 9) {
            return 9;
        } else if (fIEVersion === 10) {
            return 10;
        } else {
            return 6;//IE版本<=7
        }
    } else if (isEdge) {
        return 'edge';//edge
    } else if (isIE11) {
        return 11; //IE11
    } else {
        return false;//不是ie浏览器
    }
}

//判断是否是Firefox
export function isFirefox(){
    return (navigator.userAgent.indexOf("Firefox") > -1);
}

//封装的日期排序方法
export function ForwardRankingDate(data, p) {
    for (let i = 0; i < data.length - 1; i++) {
        for (let j = 0; j < data.length - 1 - i; j++) {
            if (Date.parse(data[j][p]) > Date.parse(data[j+1][p])) {
                let temp = data[j];
                data[j] = data[j + 1];
                data[j + 1] = temp;
            }
        }
    }
    return data;
}

//日期正序排序
export function sortDownDate(a, b) {
    return Date.parse(a) - Date.parse(b);
}

//获取台风路线的最后移动时间
export function getLastTime(products){
    let time = '', allTime = [];
    products.length > 0 && products.map(item => {
        time = item.data[0].data_time;
        item.checked && item.data.map((ktem, kndex) => {
            allTime.push(ktem.data_time);
        });
    });
    allTime.map(ptem => {
        if(moment(ptem).isAfter(moment(time))){ time = ptem; }
    });
    return time;
}

//处理选中状态雷达数据
export function handleRadarData(data){
    let newData = [];
    if(data.length > 0 ){
        newData = data.map((item, index) => {
            item.active = index === 0 ? true : false;
            return item;
        });
    }
    return newData;
}
